<?php
namespace Home\Controller;

use Util\AccessController;

class HsGroupOrderController extends AccessController {

    /*
     * 开团
     */
    public function AddGroupOrder() {
        $LS = Service('HsGroupOrder','hs_group_order');
        $data =$LS->AddGroupOrder($_POST);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 团详情
     */
    public function GroupOrderInfo(){
        $where['group_id'] = $_POST['TID'];
        $where['user_group_order_state'] = 1;
        $LS = Service('M','hs_group_order_view');
        $data = $LS->query($where);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }
    /*
     * 进团
     */
    public function JoinGroupOrder(){
        $LS1 = Service('M','hs_group_order');
        $where['ID'] = $_POST['TID'];
        $where['END_TIME'] = array('gt',date('Y-m-d H:i:s'));
        $OK  = $LS1->find($where);
        if($OK){
            $LS = Service('M','group_order_log');
            $save['ORDER_ID'] = $_POST['order_id'];
            $save['GROUP_ID'] =$_POST['TID'];
            $save['USER_ID'] =passport_decrypt($_POST['token'], C('JM_KEY'));
            $save['JOIN_TIME'] =date('Y-m-d H:i:s');
            $OK = $LS->Add($save);
            $DATA = ResData($OK);
            $this->ajaxReturn($DATA, 'JSON');
        }else{
            $DATA = ResErrorData(9);
            $this->ajaxReturn($DATA, 'JSON');
        }

    }
    /*
     * 正在进行的团列表
     */
    public function AllGroupOrderList(){
        $LS = Service('M','hs_group_order_view');
        $where['group_end_time'] = array('gt',date('Y-m-d H:i:s'));
        $data = $LS->query();
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }
    /*
     * 我的拼团列表
     */
    public function MyGroupOrderList(){
        $where['join_user_id'] =passport_decrypt($_POST['token'], C('JM_KEY'));
        $where['group_end_time'] = array('gt',date('Y-m-d H:i:s'));
        $where['user_group_order_state'] = 1;
        $join = 'ts_u_user on ts_u_user.user_id = ts_hs_group_order_view.group_user_id';
        $files = 'ts_hs_group_order_view.*,ts_u_user.user_image as group_image,
        ts_u_user.user_wx_name as group_wx_name';
        $LS = Service('M','hs_group_order_view');
        $data = $LS->join($where,$join,$files);
        foreach($data as $k =>$v){
            $LS1 = Service('BookGet','group_order_log');
            $data[$k]['group_num'] =$LS1->GroupOrderGetMoney($v['join_order_id']);
        }
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }



}